package com.fractalwrench.harmonybootstrap.sample;

import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.DANGER;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.INFO;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.PRIMARY;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.REGULAR;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.SECONDARY;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.SUCCESS;
import static com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand.WARNING;

import com.beardedhen.harmonybootstrap.BootstrapCircleThumbnail;
import com.beardedhen.harmonybootstrap.utils.ResUtil;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.PixelMapElement;

public class BootstrapCircleThumbnailExample extends AbilitySlice {

    private static final float BASELINE_SIZE = 300;

    private ComponentContainer rootLayout;

    private int resId = ResourceTable.Media_ladybird;

    private DefaultBootstrapSize size = DefaultBootstrapSize.MD;

    BootstrapCircleThumbnail imageChange, themeChange, borderChange, sizeChange, setImagePixelMap, setPixelMapRes,setImageElement;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_example_bootstrap_circle_thumbnail, null, false);
        themeChange = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_theme_change_example);
        imageChange = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_image_change_example);
        borderChange = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_border_change_example);
        sizeChange = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_size_change_example);
        setImagePixelMap = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_set_pixelmap_pixelmap_example);
        setPixelMapRes = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_set_pixelmap_resource_example);
        setImageElement = (BootstrapCircleThumbnail) rootLayout.findComponentById(ResourceTable.Id_bcircle_set_Image_Element_example);

        super.setUIContent(rootLayout);

        if (themeChange != null) {
            themeChange.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    switch ((DefaultBootstrapBrand) themeChange.getBootstrapBrand()) {
                        case PRIMARY:
                            themeChange.setBootstrapBrand(SUCCESS);
                            break;
                        case SUCCESS:
                            themeChange.setBootstrapBrand(INFO);
                            break;
                        case INFO:
                            themeChange.setBootstrapBrand(WARNING);
                            break;
                        case WARNING:
                            themeChange.setBootstrapBrand(DANGER);
                            break;
                        case DANGER:
                            themeChange.setBootstrapBrand(SECONDARY);
                            break;
                        case SECONDARY:
                            themeChange.setBootstrapBrand(REGULAR);
                            break;
                        case REGULAR:
                            themeChange.setBootstrapBrand(PRIMARY);
                            break;
                    }
                }
            });

            if (imageChange != null) {
                imageChange.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        switch (resId) {
                            case ResourceTable.Media_ladybird:
                                resId = ResourceTable.Media_caterpillar;
                                break;

                            case ResourceTable.Media_caterpillar:
                                resId = 0;
                                break;
                            case 0:
                                resId = ResourceTable.Media_ladybird;
                        }
                        imageChange.setPixelMap(resId);
                    }
                });


                if (borderChange != null) {
                    borderChange.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            borderChange.setBorderDisplayed(!borderChange.isBorderDisplayed());
                        }
                    });
                }


                if (sizeChange != null) {
                    sizeChange.setLayoutConfig(getLayoutParams(size.scaleFactor()));
                    sizeChange.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            switch (size) {
                                case XS:
                                    size = DefaultBootstrapSize.SM;
                                    break;
                                case SM:
                                    size = DefaultBootstrapSize.MD;
                                    break;
                                case MD:
                                    size = DefaultBootstrapSize.LG;
                                    break;
                                case LG:
                                    size = DefaultBootstrapSize.XL;
                                    break;
                                case XL:
                                    size = DefaultBootstrapSize.XS;
                                    break;
                            }
                            sizeChange.setBootstrapSize(size);
                            sizeChange.setLayoutConfig(getLayoutParams(size.scaleFactor()));
                        }
                    });
                }


                if (setImagePixelMap != null) {
                    setImagePixelMap.setPixelMap(ResUtil.getPixelMap(this, ResourceTable.Media_small_daffodils).get());
                }

                if (setPixelMapRes != null) {
                    setPixelMapRes.setPixelMap(ResourceTable.Media_caterpillar);
                }


                if (setImageElement != null) {
                    setImageElement.setImageElement(new PixelMapElement(ResUtil.getPixelMap(this, resId).get()));
                }
    }


}}


    private DirectionalLayout.LayoutConfig getLayoutParams(float factor) {
        float size = BASELINE_SIZE * factor;
        return new DirectionalLayout.LayoutConfig((int) size, (int) size);
    }
}
